from django.contrib.auth.models import AbstractUser
from django.db import models


# 用户数据模型
class User(AbstractUser):
    class Meta:
        verbose_name = '用户'
        verbose_name_plural = verbose_name


class UserRole(models.Model):
    user = models.ForeignKey('User', on_delete=models.CASCADE, null=False,)
    role = models.ForeignKey('Role', on_delete=models.CASCADE, null=False,)


# 系统角色
class Role(models.Model):
    id = models.AutoField(primary_key=True)
    name = models.CharField(max_length=255, null=False, default='', unique=True)
    role_type = models.CharField(max_length=255, null=False, default='')
    useable = models.IntegerField(null=False, default=1)
    remarks = models.CharField(max_length=255, null=True, default='')


# 系统菜单数据模型
class Menu(models.Model):
    id = models.AutoField(primary_key=True)
    parent_id = models.IntegerField(null=False, default=0)
    parent_name = models.CharField(max_length=255, null=True, default='')
    parent_ids = models.CharField(max_length=255, null=True, default='')
    name = models.CharField(max_length=255, null=False, default='')
    sort = models.IntegerField(null=False, default=0)
    href = models.CharField(max_length=255, null=True, default='')
    base_href = models.CharField(max_length=255, null=True, default='')
    is_show = models.IntegerField(null=False, default=0)
    type = models.IntegerField(null=False, default=0)
    has_children = models.CharField(max_length=255, null=True, default='false')
    icon = models.CharField(max_length=255, null=True, default='')
    remarks = models.CharField(max_length=255, null=True, default='')


class RoleMenu(models.Model):
    id = models.AutoField(primary_key=True)
    role = models.ForeignKey('Role',
                             null=False,
                             on_delete=models.CASCADE)

    menu = models.ForeignKey('Menu',
                             null=True,
                             on_delete=models.CASCADE)
    # -----------------------------------------------
    # 为角色赋值菜单时，父节点保存类型为0，叶节点为1，
    # 显示角色有哪些菜单权限时，只查询1
    # 首页左侧菜单显示时，查询所有
    # -----------------------------------------------
    type = models.IntegerField(null=False, default=0)
